package com.fastloan.mgt.invest.controller.comm;

import com.fastloan.mgt.invest.model.InvestType;
import com.fastloan.mgt.invest.service.InvestTypeService;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultTo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;

/**
 * 模版类型
 *
 * @author rankai
 *         createTime 2017-08-2017/8/8 10:39
 */
@RestController
@RequestMapping("/api/invest/type")
public class InvestTypeController extends BaseController<InvestType> {

    @Autowired
    private InvestTypeService investTypeService;

    /**
     * 问卷类型分页列表
     *
     * @return
     */
    @RequestMapping("/pageList")
    public ResultTo pageList(HttpServletRequest request) {
        Example example = new Example(InvestType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("topOrgId", ManagerKit.getTopOrgId())
                .andEqualTo("state", 1);
        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            criteria.andEqualTo("name", name);
        }
        return dataPage(example);
    }

    /**
     * 根据ID获取问卷类型
     *
     * @param investTypeId 问卷类型ID
     * @return
     */
    @GetMapping("/{investTypeId}")
    public ResultTo select(@PathVariable("investTypeId") Integer investTypeId) {
        InvestType investType = investTypeService.selectByPrimaryKey(investTypeId);
        if (investType.getState() == 1) {
            return new ResultTo(600, "该数据已经被删除");
        }
        return new ResultTo().setData(investType);
    }

    /**
     * 获取全部问卷类型
     *
     * @return
     */
    @GetMapping("/all")
    public ResultTo selectAll() {
        Example example = new Example(InvestType.class);
        example.createCriteria().andEqualTo("topOrgId", ManagerKit.getTopOrgId())
                .andEqualTo("state", 1);
        return new ResultTo().setData(investTypeService.selectByExample(example));
    }

    /**
     * 更新问卷类型
     *
     * @param investType 问卷类型Bean
     * @return
     */
    @PutMapping
    public ResultTo update(InvestType investType) {
        investType.setTopOrgId(null);
        investTypeService.updateByPrimaryKeySelective(investType);
        return new ResultTo();
    }

    /**
     * 增加问卷类型
     *
     * @param investType 问卷类型Bean
     * @return
     */
    @PostMapping
    public ResultTo insert(InvestType investType) {
        investType.setTopOrgId(ManagerKit.getTopOrgId().toString());
        investType.setState(1);
        investTypeService.insertSelective(investType);
        return new ResultTo();
    }

    /**
     * 判断类型名是否存在
     *
     * @param name 类型名
     * @return
     */
    @GetMapping(params = "name")
    public ResultTo haveName(@RequestParam("name") String name) {
        InvestType investType = new InvestType();
        investType.setState(1);
        investType.setName(name);
        investType.setTopOrgId(ManagerKit.getTopOrgId().toString());
        if (investTypeService.selectCount(investType) > 0) {
            return new ResultTo().setData(false);
        }
        return new ResultTo().setData(true);
    }
}
